<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="新闻名称" prop="title">
        <el-input v-model="queryParams.title" placeholder="请输入新闻名称" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:post:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:post:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:post:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="DataList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="新闻名称" align="center" prop="title">
        <template slot-scope="scope">
          {{scope.row.title}}
        </template>
      </el-table-column>
      <el-table-column label="封面图" align="center" prop="thumb" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <img :src="scope.row.thumb" width="50" />
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="created_at" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          {{parseTime(scope.row.created_at)}}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:post:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:post:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.page" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加开始 -->
    <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open"  append-to-body>
      <el-form ref="form" :model="form" label-width="120px" style="height: 500px;overflow-y: scroll;">
        <el-form-item label="新闻名称" prop="title">
          <el-input v-model="form.title" placeholder="请输入新闻名称" clearable size="small"/>
        </el-form-item>
        <el-form-item label="封面图" prop="thumb">
          <el-upload :action="upload_url" :headers="headers" :before-upload="handleSetHeaders" :on-success="handleFileSuccess" :show-file-list="false" class="avatar-uploader">
            <img v-if="form.thumb!==undefined" :src="form.thumb" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <Editor v-model="form.content" minHeight=400 placeholder="请输入内容" clearable size="small"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 添加结束 -->
    <el-dialog :visible.sync="dialogVisible">
      <img width="100%" :src="dialogImageUrl" alt="">
    </el-dialog>
  </div>
</template>

<script>
import * as News from "@/api/operation/news";
import * as Auth from '@/utils/auth'

  export default {
    name: "News",
    data() {
      return {
        upload_url: process.env.VUE_APP_BASE_URL + "/file/upload",
        headers: Auth.getHeader(),
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        open: false,
        form: {},
        rules: [],
        title: "",
        // 日期范围
        dateRange: [],
        DataList: [],
        // 查询参数
        queryParams: {
          page: 1,
          pageSize: 10
        },
        dialogVisible:false,
        dialogImageUrl:undefined
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询广告列表 */
      getList() {
        this.loading = true;
        News.List(this.addDateRange(this.queryParams,this.dateRange)).then(response => {
          this.DataList = response.data.rows;
          this.total = response.data.total;
          this.loading = false;
        });
      },
      // 表单重置
      reset() {
        this.form = {};
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.page = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.dateRange=[];
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length!=1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加新闻";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const Id = row.id || this.ids
        News.Show(Id).then(response => {
          this.form = response.data;
          this.form.rewards=response.data.rewards;
          this.open = true;
          this.title = "修改新闻";
        });
      },
      /** 提交按钮 */
      submitForm: function() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != undefined) {
              News.Update(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              News.Add(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const Id = row.id || this.ids;
        this.$modal.confirm('是否确认删除编号为"' + Id + '"的数据项？').then(function() {
          return News.Delete(Id);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      handleSetHeaders(){
        this.headers=Auth.getHeader();
      },
      /****文件上传*****/
      handleFileSuccess(res) {
        this.form.thumb = res.data;
      }
    },
  };
</script>
<style>
  .avatar-uploader{
    width: 100px;
  }
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }

  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 100px;
    height: 100px;
    line-height: 100px;
    text-align: center;
  }

  .avatar {
    width: 100px;
    height: 100px;
    display: block;
  }
</style>
